import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import { ReviewService } from "./review.service";
import { InvestService } from "../../invest.service";
import { RemindComponent } from "../remind/remind.component";
import * as _ from 'lodash';

@Component({
  selector: 'app-review',
  templateUrl: './review.component.html',
  styleUrls: ['./review.component.scss'],
  providers: [ReviewService]
})
export class ReviewComponent implements OnInit {
  // 提醒者模版
  @ViewChild("reviewRemind", { read: ViewContainerRef })
  reviewRemind: ViewContainerRef;
  remindComponent: ComponentRef<RemindComponent>;
  // 操作
  public operate: any;
  //短信提醒
  public sSMSFlag: any = false;
  //邮件提醒
  public sEmailFlag: any = false;
  // 列表数据
  public dataList: any = [];
  // 提醒信息
  public auditOprZh: any;
  public remindBody: any = {
    auditOpr: '',
    title: '',
    content: ''
  };

  constructor(
    public reviewService: ReviewService,
    public resolver: ComponentFactoryResolver,
    public investService: InvestService
  ) { }

  ngOnInit() {
    this.defaultValue();
  }
  ngAfterViewInit() {
    let that = this;
    // 用于接收提醒者数据
    this.investService.remindData = new EventEmitter();
    this.investService.remindData.subscribe((data: any) => {
      if (data) {
        that.remindBody.auditOpr = _.cloneDeep(data.userChecked);
        that.auditOprZh = _.cloneDeep(data.displayName);
      }
    })
  }
  // 复核
  review() {
    let that = this;
    let checkedItem = [];
    _.forEach(this.dataList, item => {
      // 防止删除其他操作有用属性，另起变量赋值
      let it = _.clone(item);
      // 产品内码赋值给nIxId，假设是新增，后台需要这个字段进行关联
      it.nIxId = it.nFundNo;
      delete it.sFundType;
      delete it.sFundCode;
      delete it.sFullName;
      delete it.sStatus;
      delete it.changeInitiator;
      delete it.changeDate;
      delete it.reviewer;
      delete it.auditDate;
      delete it.sOprStateZh;
      delete it.sStep;
      delete it.isChecked;
      delete it.nFundNo;
      delete it.riskAuditOpr;
      delete it.riskAuditDate;
      delete it.sStepZh;
      delete it.sFundTypeZh;
      delete it.sFlowStepZh;
      delete it.sOprZh;
      checkedItem.push(it);
    });
    window['swal']({
      type: "warning",
      showCancelButton: true,
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      closeOnConfirm: false,
      showLoaderOnConfirm: true,
      title: "提示",
      text: "是否确认复核?",
      html: true
    }, function (isConfirm) {
      if (isConfirm) {
        let postBody: any = {
          auditContentBean: that.remindBody,
          pmsInvestJournal: checkedItem
        }
        that.reviewService.review(postBody).subscribe(data => {
          if (data) {
            window['$']('#reviews').modal('hide');
            window['swal']("成功", "复核成功", "success");
            that.investService.targetReview.emit('refresh');
          }
        })
      }
    })
  }
  // 返回
  reback() {
    let that = this;
    let checkedItem = [];
    _.forEach(this.dataList, item => {
      // 防止删除其他操作有用属性，另起变量赋值
      let it = _.clone(item);
      // 产品内码赋值给nIxId，假设是新增，后台需要这个字段进行关联
      it.nIxId = it.nFundNo;
      delete it.sFundType;
      delete it.sFundCode;
      delete it.sFullName;
      delete it.sStatus;
      delete it.changeInitiator;
      delete it.changeDate;
      delete it.reviewer;
      delete it.auditDate;
      delete it.sOprStateZh;
      delete it.sStep;
      delete it.isChecked;
      delete it.nFundNo;
      delete it.riskAuditOpr;
      delete it.riskAuditDate;
      delete it.sStepZh;
      delete it.sFundTypeZh;
      delete it.sFlowStepZh;
      delete it.sOprZh;
      checkedItem.push(it);
    });
    window['swal']({
      type: "warning",
      showCancelButton: true,
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      closeOnConfirm: false,
      showLoaderOnConfirm: true,
      title: "提示",
      text: "是否确认返回?",
      html: true
    }, function (isConfirm) {
      if (isConfirm) {
        let postBody: any = {
          auditContentBean: that.remindBody,
          pmsInvestJournal: checkedItem
        }
        that.reviewService.reback(postBody).subscribe(data => {
          if (data) {
            window['$']('#reviews').modal('hide');
            window['swal']("成功", "返回成功", "success");
            that.investService.targetReview.emit('refresh');
          }
        })
      }
    })

  }
  // 接收者列表
  showRemind() {
    if (this.remindComponent && this.remindComponent.instance) {
      this.remindComponent.destroy();
    }
    const remindComponent = this.resolver.resolveComponentFactory(RemindComponent);
    this.remindComponent = this.reviewRemind.createComponent(remindComponent);
    window['$']('#reviewRemind').modal('show');
  }
  //获取默认模板接口
  defaultValue() {
    let that = this;
    let getBody: any = {
      "sButtonId": '70142@SamInvestJCDReExamine_btn_audit'
    }
    this.reviewService.defaultValue(getBody).subscribe(data => {
      if (data) {
        console.log(data);
        if (Array.isArray(data)) {
          if (data.length > 1) {
            that.auditOprZh = data[0].sContentZh;
            that.remindBody.auditOpr = data[0].sContent;
            if (data[0].sSMSFlag == 'true') {
              that.sSMSFlag = true;
            }
            if (data[0].sEmailFlag == 'true') {
              that.sEmailFlag = true;
            }
            // that.remindBody.sSMSContent = data[1].sContent;
            that.remindBody.content = data[1].sContent;
            that.remindBody.title = data[1].sRemindTitle;
          }
        }
      }
    })
  }
  //设置默认接口
  setDefault() {
    let that = this;
    let postBody = _.cloneDeep(this.remindBody);
    postBody['sButtonId'] = '70142@SamInvestJCDReExamine_btn_audit';
    postBody['sEmailFlag'] = _.cloneDeep(this.sEmailFlag);
    postBody['sEmailContent'] = _.cloneDeep(postBody.content);
    postBody['sSMSFlag'] = _.cloneDeep(this.sSMSFlag);
    postBody['sSMSContent'] = _.cloneDeep(postBody.content);
    delete postBody.content;
    if (postBody.sEmailFlag) {
      postBody.sEmailFlag = 'Y'
    } else {
      postBody.sEmailFlag = 'N'
    }
    if (postBody.sSMSFlag) {
      postBody.sSMSFlag = 'Y'
    } else {
      postBody.sSMSFlag = 'N'
    }
    console.log(postBody)
    window['swal']({
      type: "warning",
      showCancelButton: true,
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      closeOnConfirm: false,
      showLoaderOnConfirm: true,
      title: "提示",
      text: "<div style='max-height: 200px;overflow-y: auto;'>" + "请确认是否设置默认" + "</div>",
      html: true
    },
      function (isConfirm) {
        if (isConfirm) {
          that.reviewService.setDefault(postBody).subscribe(data => {
            if (data) {
              window['swal']("提示", "设置默认成功", "success");
            }

          })
        }
      });
    console.log(postBody)
  }
}
